/**
  ******************************************************************************
  * @file    gt32f030_lptim.c
  * @author  GT Application Team
  * @version V1.0.0
  * @date    03-January-2025      
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2022 Giantec Semicondutor Inc</center></h2>
  *
  *             http://www.giantec-semi.com/
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/  
#include "gt32f030_rcc.h"
#include "gt32f030_lptim.h"


/** @addtogroup GT32F030_StdPeriph_Driver
  * @{
  */
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define LPTIM_INTSR_IT     ((uint32_t)0x00000001)
#define LPTIM_CR_IT        ((uint32_t)0x00000100)
#define LPTIM_CR_RUN       ((uint32_t)0x00000001)


/** @defgroup LPTIM 
  * @brief LPTIM driver modules
  * @{
  */ 

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/** @defgroup LPTIM_Private_Functions
  * @{
  */ 

/** @defgroup LPTIM_Init Initialization functions
 *  @brief   Initialization and functions
 *
@verbatim    
 ===============================================================================
              ##### Initialization functions #####
 ===============================================================================
@endverbatim
  * @{
  */

/**
  * @brief  Deinitializes the LPTIM peripheral registers to their default reset values.
  * @retval None
  */
void LPTIM_DeInit(void)
{
	RCC_PeriphResetCmd(RCC_PeriphReset_LPTIM, ENABLE);
	RCC_PeriphResetCmd(RCC_PeriphReset_LPTIM, DISABLE);
}


void DelayX(unsigned int t)
{
	//unsigned int i;
	while(t--);
}
/**
  * @brief  Initializes the LPTIM peripheral according to the specified
  *         parameters in the LPTIM_InitStruct .
  * @param  LPTIM_InitStruct: pointer to a LPTIM_InitTypeDef structure that contains
  *         the configuration information.
  * @retval None
  */
void LPTIM_Init(LPTIM_InitTypeDef* LPTIM_InitStruct)
{
  /* Check the parameters */	
	assert_param(IS_FUNCTIONAL_STATE(LPTIM_InitStruct->LPTIM_CLKSourceCmd));
	assert_param(IS_FUNCTIONAL_STATE(LPTIM_InitStruct->LPTIM_ITCmd));
	assert_param(IS_BTIM_GATEPOLE(LPTIM_InitStruct->LPTIM_GatePole));
	assert_param(IS_FUNCTIONAL_STATE(LPTIM_InitStruct->LPTIM_GateCmd));
	assert_param(IS_BTIM_CLKPRSC(LPTIM_InitStruct->LPTIM_CLKSource));
	assert_param(IS_FUNCTIONAL_STATE(LPTIM_InitStruct->LPTIM_ToggleCmd));	
	assert_param(IS_BTIM_FUNCTIONMODE(LPTIM_InitStruct->LPTIM_FunctionMode));
	assert_param(IS_BTIM_LOADMODE(LPTIM_InitStruct->LPTIM_LoadMode));
	
	LPTIM->CR =  (LPTIM_InitStruct->LPTIM_CLKSourceCmd<<9)  | \
	             (LPTIM_InitStruct->LPTIM_ITCmd<<8)         | \
	             (LPTIM_InitStruct->LPTIM_GatePole<<7)      | \
	             (LPTIM_InitStruct->LPTIM_GateCmd<<6)       | \
	             (LPTIM_InitStruct->LPTIM_CLKSource<<4)     | \
	             (LPTIM_InitStruct->LPTIM_ToggleCmd<<3)     | \
	             (LPTIM_InitStruct->LPTIM_FunctionMode<<2)  | \
	             (LPTIM_InitStruct->LPTIM_LoadMode<<1);
								
  LPTIM->LOAD    = LPTIM_InitStruct->LPTIM_LoadVal;
	while(LPTIM->CR&0x10000){};	  //WT_FLAG
	//DelayX(1000000);
  LPTIM->BGLOAD  = LPTIM_InitStruct->LPTIM_BGLoadVal;	
	while(LPTIM->CR&0x10000);	  //WT_FLAG
	//DelayX(1000000);
}

/**
  * @brief  Fills each LPTIM_InitStruct member with its default value.
  * @param  LPTIM_InitStruct: pointer to a LPTIM_InitTypeDef structure
  *         which will be initialized.
  * @retval None
  */
void LPTIM_StructInit(LPTIM_InitTypeDef* LPTIM_InitStruct)
{
	LPTIM_InitStruct->LPTIM_CLKSourceCmd = LPTIM_CLKSourceCmd_Disable;
	LPTIM_InitStruct->LPTIM_ITCmd        = DISABLE;
	LPTIM_InitStruct->LPTIM_GatePole     = LPTIM_GatePole_Low;
	LPTIM_InitStruct->LPTIM_GateCmd      = DISABLE;
	LPTIM_InitStruct->LPTIM_CLKSource    = LPTIM_CLKSource_PClk;
	LPTIM_InitStruct->LPTIM_ToggleCmd    = DISABLE;
	LPTIM_InitStruct->LPTIM_FunctionMode = LPTIM_FunctionMode_Timer;
	LPTIM_InitStruct->LPTIM_LoadMode     = LPTIM_LoadMode_FreeRun;

  LPTIM_InitStruct->LPTIM_LoadVal     = 0x00;
  LPTIM_InitStruct->LPTIM_BGLoadVal   = 0x00;
}

/**
  * @}  
  */





/** @defgroup LPTIM_OpCfg Operation and Configuration functions
 *  @brief   Operation and Configuration functions
 *
@verbatim   
 ===============================================================================
           ##### Operation and Configuration functions #####
 ===============================================================================  
@endverbatim
  * @{
  */

/**
  * @brief  Enables or disables the LPTIM .
  * @param  NewState: new state of the LPTIM.
  *         This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void LPTIM_Cmd(FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONAL_STATE(NewState));

  if (NewState != DISABLE)
  {
    LPTIM->CR |= LPTIM_CR_RUN;
  }
  else
  {
    LPTIM->CR &= ~LPTIM_CR_RUN;
  }	
}

/**
  * @brief  Set the LPTIM load value.
  * @param  LoadVal: the LPTIM load value
  * @retval None
  */
void LPTIM_SetLoadVal(uint32_t LoadVal)
{
  LPTIM->LOAD = LoadVal;
	while(LPTIM->CR&0x10000);	  //WT_FLAG
}

/**
  * @brief  Set the LPTIM background load value.
  * @param  BGLoadVal: the LPTIM background load value
  * @retval None
  */
void LPTIM_SetBGLoadVal(uint32_t BGLoadVal)
{ 
	LPTIM->BGLOAD = BGLoadVal;
	while(LPTIM->CR&0x10000);	  //WT_FLAG
}


/**
  * @brief  Get the LPTIM counter value.
  * @param  None.
  * @retval the LPTIM counter value
  */
uint32_t LPTIM_GetCountVal(void)
{
	return LPTIM->CNT;
}

/**
  * @}  
  */



/** @defgroup  LPTIM_IntrFlag Interrupts and flags management functions
 *  @brief    Interrupts and flags management functions
 *
@verbatim   
 ===============================================================================
           #####  Interrupts and flags management functions #####
 ===============================================================================  
@endverbatim
  * @{
  */
/**
  * @brief  Enables or disables the LPTIM interrupts.
  * @param  NewState: new state of the LPTIM interrupt.
  *         This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void LPTIM_ITConfig(FunctionalState NewState)
{
  assert_param(IS_FUNCTIONAL_STATE(NewState));

  if (NewState != DISABLE)
  {
    LPTIM->CR |= LPTIM_CR_IT;
  }
  else
  {
    LPTIM->CR &= ~LPTIM_CR_IT;
  }		
}

/**
  * @brief  Checks whether the LPTIM interrupt has occurred or not.
  * @param  None
  * @retval The new state of PCA_IT (SET or RESET).
  */
ITStatus LPTIM_GetIT(void)
{
  ITStatus bitstatus = RESET;
	
  if ((LPTIM->INTSR & LPTIM_INTSR_IT) != (uint8_t)RESET)
  {
    bitstatus = SET;
  }
  else
  {
    bitstatus = RESET;
  }
  return  bitstatus;		
}

/**
  * @brief  Clears the LPTIM interrupt flags.
  * @param  None
  * @retval None.
  */
void LPTIM_ClearIT(void)
{
	LPTIM->INTCLR = 0x01;
}

/**
  * @}  
  */ 

/**
  * @}  
  */

/**
  * @}  
  */ 

/**
  * @}
  */ 
/************************ (C) COPYRIGHT Giantec Semicondutor Inc *****END OF FILE****/
